package kr.ac.kaist.ysproject;

import kr.ac.kaist.ysproject.data.DataProfile;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class ContactListActivity extends Activity {
	Activity act = this;
	private ListView mContactList;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.d("ContactListActivity", "OnCreate() started");
    	
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_list);
        
        mContactList = (ListView)findViewById(R.id.contactListView);
    }
    
    
    @Override
	public void onStart() {
    	super.onStart();
		Log.d("ContactListActivity", "onStart() started");
		
		Cursor cursor = getContacts();
        String[] fields = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.LOOKUP_KEY
        };
                
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_list_row, cursor,
                fields, new int[] {R.id.contactNameTextView, R.id.contactLookupKeyTextView});
        mContactList.setAdapter(adapter);
        
        mContactList.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView parent, View view, int position, long id){
//        		Log.d("Click", ((TextView)view.findViewById(R.id.contactNameTextView)).getText().toString());
        		
        		Uri uri2 = ContactsContract.Data.CONTENT_URI;
        		String[] projection = null;
                String selection = ContactsContract.Data.LOOKUP_KEY + " = '" + ((TextView)view.findViewById(R.id.contactLookupKeyTextView)).getText() + "'";
                String[] selectionArgs = null;
                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
                Cursor cursor2 = managedQuery(uri2, projection, selection, selectionArgs, sortOrder);
                
                DataProfile dataProfile = new DataProfile();
                dataProfile.setName(((TextView)view.findViewById(R.id.contactNameTextView)).getText().toString());
                
                while (cursor2.moveToNext()) {
                	
                  String type = cursor2.getString(cursor2.getColumnIndex(ContactsContract.Data.MIMETYPE));
                                    
                  // 폰번호
                  if(type.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
//                	  Log.d("contact", "phone" + " : " + cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
                	  dataProfile.setMobileNo(cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
                  }
                  
                  // 이메일
                  if(type.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
//                      Log.d("contact", "email" + " : " + cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
                      dataProfile.setEmail(cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
                  }
                  
                  // 사진
//                  if(type.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) {
//                      Log.d("contact", "phone number" + " : " + cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO)));
//                  	
//                  }
              }                
        		
        		Intent intent = new Intent(view.getContext(), ProfileActivity.class);
        		intent.putExtra("ObjectData", dataProfile);
        		view.getContext().startActivity(intent);
        		act.finish();
        	}
		});
    }
    
    
    private Cursor getContacts() {
    	
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.LOOKUP_KEY
        };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }
    
}
